package mybatis.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import mybatis.entity.UserEntity;
import mybatis.mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author jackcfan
 * @date 22/9/30
 */
@Service
public class UserServiceImpl implements UserService {
  @Resource
  UserMapper userMapper;

  @Transactional(rollbackFor = Exception.class)
  public void update(UserEntity user){
    // 做两次更新测试事务
    user.updateById();
    new UserEntity().update(
            Wrappers.lambdaUpdate(UserEntity.class)
                    .set(UserEntity::getUpdateTime, new Date())
                    .eq(UserEntity::getId,user.getId())
    );
    if(user.getAge() > 60){
      throw new IllegalArgumentException("age>60");
    }
  }

  public void updateWithoutTransaction(UserEntity user) {
    update(user);
  }
}
